package com.zzh.net.Interceptor;


import com.zzh.net.Utils.PackInfoUtils;
import com.zzh.tools.sp.SPKey;
import com.zzh.tools.sp.ToolSpUtils;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class CommonInterceptor implements Interceptor {


    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        Request request;
        HttpUrl.Builder builder = originalRequest.url().newBuilder()
                // Provide your custom parameter here
                .addQueryParameter("m_", "android")
                .addQueryParameter("pt_", "androidfxc")
                .addQueryParameter("v_", PackInfoUtils.getVersionCode() + "");
        if (ToolSpUtils.getMark(SPKey.IS_LOGIN)) {
            String userID = ToolSpUtils.getString(SPKey.USER_ID);
            builder.addQueryParameter("userId", userID)
                    .addQueryParameter("uid_", userID)
                    .addQueryParameter("access_token", ToolSpUtils.getString(SPKey.TOKEN));
        }
        request = originalRequest.newBuilder().url(builder.build())
                .addHeader("Connection", "close")
                .addHeader("User-Agent", "Android")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .build();
        return chain.proceed(request);
    }
}
